<!DOCTYPE html><html lang="zh" >


<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  
  
    <meta name="generator" content="Wowchemy 5.6.0 for Hugo" />
  

  
  












  
  










  







  
  
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  

  
  
  
    
      
      <link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Merriweather&family=Roboto+Mono&display=swap">
      <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Merriweather&family=Roboto+Mono&display=swap" media="print" onload="this.media='all'">
    
  

  
  
  
  
  
  

  

  
  
  
    
  
  <meta name="description" content="简介 本文旨在完成nginx的安装以及前端和后端的转发配置（以" />

  
  <link rel="alternate" hreflang="zh" href="https://songchuanrang.gitee.io/docs/nginx/" />

  
  
  
    <meta name="theme-color" content="#1565c0" />
  

  
  

  

  <link rel="stylesheet" href="/css/vendor-bundle.min.c7b8d9abd591ba2253ea42747e3ac3f5.css" media="print" onload="this.media='all'">

  
  
  
    
    

    
    
    
    
      
      
    
    
    

    
    
    

    

    
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
      
      

      
      
        
      

      
    
      
      

      
      

      
    
      
      

      
      

      
    
  

  
  
  
  
  
  
  <link rel="stylesheet" href="/css/wowchemy.50151d350c96d255e89f810190000890.css" />

  
  
  
  
  
  
  
    
    
    <link rel="stylesheet" href="/css/libs/chroma/github-light.min.css" title="hl-light" media="print" onload="this.media='all'" >
    <link rel="stylesheet" href="/css/libs/chroma/dracula.min.css" title="hl-dark" media="print" onload="this.media='all'" disabled>
  

  
  



  


  


  




  
  
  

  
  

  
  
    <link rel="manifest" href="/manifest.webmanifest" />
  

  
  <link rel="icon" type="image/png" href="/media/icon_huec9f8681c90e5958ea60dec1a7d5fe8a_15532_32x32_fill_lanczos_center_3.png" />
  <link rel="apple-touch-icon" type="image/png" href="/media/icon_huec9f8681c90e5958ea60dec1a7d5fe8a_15532_180x180_fill_lanczos_center_3.png" />

  <link rel="canonical" href="https://songchuanrang.gitee.io/docs/nginx/" />

  
  
  
  
  
  
  
  
    
    
  
  

  
  
    
    
  
  <meta property="twitter:card" content="summary" />
  
    <meta property="twitter:site" content="@wowchemy" />
    <meta property="twitter:creator" content="@wowchemy" />
  
  <meta property="og:site_name" content="学习分享" />
  <meta property="og:url" content="https://songchuanrang.gitee.io/docs/nginx/" />
  <meta property="og:title" content="nginx安装及配置 | 学习分享" />
  <meta property="og:description" content="简介 本文旨在完成nginx的安装以及前端和后端的转发配置（以" /><meta property="og:image" content="https://songchuanrang.gitee.io/media/icon_huec9f8681c90e5958ea60dec1a7d5fe8a_15532_512x512_fill_lanczos_center_3.png" />
    <meta property="twitter:image" content="https://songchuanrang.gitee.io/media/icon_huec9f8681c90e5958ea60dec1a7d5fe8a_15532_512x512_fill_lanczos_center_3.png" /><meta property="og:locale" content="zh" />
  
    
      <meta
        property="article:published_time"
        content="2022-04-03T20:35:25&#43;08:00"
      />
    
    <meta property="article:modified_time" content="2022-04-03T20:35:25&#43;08:00">
  

  



  

  


  <title>nginx安装及配置 | 学习分享</title>

  
  
  
  











</head>


<body id="top" data-spy="scroll" data-offset="70" data-target="#TableOfContents" class="page-wrapper   " data-wc-page-id="06173d5fd9bd02d404b444d5d72f6db2" >

  
  
  
  
  
  
  
  
  
  <script src="/js/wowchemy-init.min.613040fe4f2c0f007b4dcb64404201cb.js"></script>

  


<aside class="search-modal" id="search">
  <div class="container">
    <section class="search-header">

      <div class="row no-gutters justify-content-between mb-3">
        <div class="col-6">
          <h1>搜索</h1>
        </div>
        <div class="col-6 col-search-close">
          <a class="js-search" href="#" aria-label="Close"><i class="fas fa-times-circle text-muted" aria-hidden="true"></i></a>
        </div>
      </div>

      <div id="search-box">
        
        <input name="q" id="search-query" placeholder="搜索..." autocapitalize="off"
        autocomplete="off" autocorrect="off" spellcheck="false" type="search" class="form-control"
        aria-label="搜索...">
        
      </div>

      
      

      

    </section>
    <section class="section-search-results">

      <div id="search-hits">
        
      </div>

    </section>
  </div>
</aside>



  <div class="page-header">
    












<header class="header--fixed">
  <nav class="navbar navbar-expand-lg navbar-light compensate-for-scrollbar" id="navbar-main">
    <div class="container-xl">

      
      <div class="d-none d-lg-inline-flex">
        <a class="navbar-brand" href="/">学习分享</a>
      </div>
      

      

      
      <div class="navbar-brand-mobile-wrapper d-inline-flex d-lg-none">
        <a class="navbar-brand" href="/">学习分享</a>
      </div>
      

      
      
      <div class="navbar-collapse main-menu-item collapse justify-content-start" id="navbar-content">

        
        <ul class="navbar-nav d-md-inline-flex">
          

        

          
        </ul>
      </div>

      <ul class="nav-icons navbar-nav flex-row ml-auto d-flex pl-md-2">

        
        

        
        
        <li class="nav-item">
          <a class="nav-link js-search" href="#" aria-label="搜索"><i class="fas fa-search" aria-hidden="true"></i></a>
        </li>
        

        
        
        
        <li class="nav-item dropdown theme-dropdown">
          <a href="#" class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-label="Display preferences">
            <i class="fas fa-moon" aria-hidden="true"></i>
          </a>
          <div class="dropdown-menu">
            <a href="#" class="dropdown-item js-set-theme-light">
              <span>浅色</span>
            </a>
            <a href="#" class="dropdown-item js-set-theme-dark">
              <span>深色</span>
            </a>
            <a href="#" class="dropdown-item js-set-theme-auto">
              <span>自动</span>
            </a>
          </div>
        </li>
        

        
        

      </ul>

    </div>
  </nav>
</header>


  </div>

  <div class="page-body">
    
    
    

    




<div class="container-fluid docs">
  <div class="row flex-xl-nowrap">
    <div class="col-12 col-md-3 col-xl-2 docs-sidebar">
      <form class="docs-search d-flex align-items-center">
  <button class="btn docs-toggle d-md-none p-0 mr-md-3 w-100" type="button" data-toggle="collapse" data-target="#docs-nav" aria-controls="docs-nav" aria-expanded="false" aria-label="Toggle section navigation">
    <div class="d-flex">
      <span class="d-md-none pl-1 flex-grow-1 text-left overflow-hidden">
        
        
          Docs
        
      </span>
      <span><i class="fas fa-chevron-down"></i></span>
    </div>
  </button>

  
  <button class="form-control sidebar-search js-search d-none d-md-flex">
    <i class="fas fa-search pr-2"></i>
    <span class="sidebar-search-text">搜索...</span>
    <span class="sidebar-search-shortcut">/</span>
  </button>
  
</form>

<nav class="collapse docs-links" id="docs-nav">
  
  
  
  
  
  

  
  
    





  
    
    
    
    
      
    
    

    
      <div class="docs-toc-item">
        <a class="docs-toc-link " href="/docs/">Docs</a>
    
      
        <ul class="nav docs-sidenav">
      


  <li class=""><a href="/docs/tomcat/">tomcat</a></li>



  <li class=""><a href="/docs/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a></li>



  <li class=""><a href="/docs/centos%E6%8C%82%E8%BD%BDiso%E6%96%87%E4%BB%B6%E4%BD%9C%E4%B8%BAyum%E5%AE%89%E8%A3%85%E6%BA%90/">centos挂载iso文件作为yum安装源</a></li>



  <li class=""><a href="/docs/java-nio/">JAVA NIO</a></li>



  <li class=""><a href="/docs/div%E5%B8%83%E5%B1%80/">div盒子模型及html布局</a></li>



  <li class="active"><a href="/docs/nginx/">nginx安装及配置</a></li>



  <li class=""><a href="/docs/gc%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90gcviewer/">GC日志分析之GCViewer</a></li>

      
        </ul>
      
    

    
      </div>
    

</nav>

    </div>

    
    
    <div class="d-none d-xl-block col-xl-3 docs-toc">
      












      <ul class="nav toc-top">
        <li><a href="#" id="back_to_top" class="docs-toc-title">在本页</a></li>
      </ul>

      <nav id="TableOfContents">
  <ul>
    <li><a href="#简介">简介</a></li>
    <li><a href="#获取安装包">获取安装包</a></li>
    <li><a href="#安装nginx">安装nginx</a></li>
    <li><a href="#配置nginx">配置nginx</a></li>
    <li><a href="#启动nginx">启动nginx</a></li>
  </ul>
</nav>

      











    </div>
    

    <main class="col-12 col-md-9 col-xl-7 py-md-3 pl-md-5 docs-content" role="main">

      <article class="article">

        <div class="docs-article-container">
          
            
  <nav class="d-none d-md-flex" aria-label="breadcrumb">
    <ol class="breadcrumb">
      
  
    
  
    
  

    <li class="breadcrumb-item">
      <a href="/">
        
          Home
        
      </a>
    </li>
  

    <li class="breadcrumb-item">
      <a href="/docs/">
        
          Docs
        
      </a>
    </li>
  

      <li class="breadcrumb-item active" aria-current="page">
        nginx安装及配置
      </li>
    </ol>
  </nav>




          
        </div>

        
        

        <div class="docs-article-container">
          
          <h1>nginx安装及配置</h1>

          <div class="article-style">

            
            

            <h2 id="简介">简介</h2>
<p>本文旨在完成nginx的安装以及前端和后端的转发配置（以linux为例）</p>
<h2 id="获取安装包">获取安装包</h2>
<p>nginx官网：https://nginx.org/en/download.html<br>
下载其中的Mainline version</p>
<h2 id="安装nginx">安装nginx</h2>
<p>首先解压至任意路径，然后cd进入解压后的文件夹，执行以下命令：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">./configure --prefix<span class="o">=</span>/usr/local/nginx --with-http_ssl_module 
</span></span></code></pre></div><p>这一步是用来配置要安装的nginx，其中，</p>
<ul>
<li><code>--prefix=/usr/local/nginx</code> 指定安装路径;</li>
<li><code>--with-http_ssl_module</code> 支持https;</li>
</ul>
<blockquote>
<p>对于安装好的nginx，查看./configure后指定的参数可使用以下命令：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"> ./sbin/nginx -V
</span></span></code></pre></div></blockquote>
<p>再执行一下命令：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">make install
</span></span></code></pre></div><p>这样，nginx就安装好了，可以进入nginx的安装路径，执行相应的路由转发配置了</p>
<h2 id="配置nginx">配置nginx</h2>
<p>nginx的配置在文件conf/nginx.conf中<br>
基本结构:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">http {
</span></span><span class="line"><span class="cl">    # 配置http
</span></span><span class="line"><span class="cl">    server {
</span></span><span class="line"><span class="cl">        # 监听端口
</span></span><span class="line"><span class="cl">        listen       80;
</span></span><span class="line"><span class="cl">        server_name  localhost;
</span></span><span class="line"><span class="cl">        # 指定要拦截的路径 
</span></span><span class="line"><span class="cl">        location / { 
</span></span><span class="line"><span class="cl">            root   html;
</span></span><span class="line"><span class="cl">            index /index.html;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    # 配置https
</span></span><span class="line"><span class="cl">    server {
</span></span><span class="line"><span class="cl">        listen       443 ssl;
</span></span><span class="line"><span class="cl">        server_name  songchuanrang.space;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        ssl_certificate      songchuanrang.space_bundle.crt;
</span></span><span class="line"><span class="cl">        ssl_certificate_key  songchuanrang.space.key;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        ssl_session_timeout  5m;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        ssl_protocols TLSv1.2 TLSv1.3;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
</span></span><span class="line"><span class="cl">        ssl_prefer_server_ciphers  on;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        location / {
</span></span><span class="line"><span class="cl">            root   html;
</span></span><span class="line"><span class="cl">            index  index.html index.htm;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        error_page   500 502 503 504  /50x.html;
</span></span><span class="line"><span class="cl">        location = /50x.html {
</span></span><span class="line"><span class="cl">            root   html;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        location /api/ {
</span></span><span class="line"><span class="cl">            proxy_pass http://gateway/;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p>配置前端项目</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">server {
</span></span><span class="line"><span class="cl"># 指定前端编译后的路径
</span></span><span class="line"><span class="cl">    root /path/to/dist;
</span></span><span class="line"><span class="cl">    location / {
</span></span><span class="line"><span class="cl">        # 前端项目默认主页
</span></span><span class="line"><span class="cl">        index /index.html;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p>配置后端项目</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">http {
</span></span><span class="line"><span class="cl">    # 反向代理的后端项目(gateway为自定义的字符串)
</span></span><span class="line"><span class="cl">    upstream gateway {
</span></span><span class="line"><span class="cl">        # 指定后端项目的ip:port
</span></span><span class="line"><span class="cl">        server 127.0.0.1:9632;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    server {
</span></span><span class="line"><span class="cl">    # 指定要拦截的请求路径，把location后的路径（包括“/”），替换为proxy_pass后指定的路径（同样包含“/”）
</span></span><span class="line"><span class="cl">    location /api/ {
</span></span><span class="line"><span class="cl">        # 指定后端项目
</span></span><span class="line"><span class="cl">        proxy_pass http://gateway/;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><h2 id="启动nginx">启动nginx</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># 启动</span>
</span></span><span class="line"><span class="cl">sbin/nginx
</span></span><span class="line"><span class="cl"><span class="c1"># 检查配置文件合法性 </span>
</span></span><span class="line"><span class="cl">sbin/nginx -t
</span></span><span class="line"><span class="cl"><span class="c1"># nginx 热更新配置</span>
</span></span><span class="line"><span class="cl">sbin/nginx -s reload
</span></span><span class="line"><span class="cl"><span class="c1"># fast shutdown</span>
</span></span><span class="line"><span class="cl">nginx -s stop
</span></span><span class="line"><span class="cl"><span class="c1"># graceful shutdown</span>
</span></span><span class="line"><span class="cl">nginx -s quit
</span></span></code></pre></div>

          </div>

          



          
          
          <div class="article-widget">
            
<div class="post-nav">
  
  
  
  <div class="post-nav-item">
    <div class="meta-nav">上一页</div>
    <a href="/docs/java-nio/" rel="next">JAVA NIO</a>
  </div>
  
  
  
  <div class="post-nav-item">
    <div class="meta-nav">下一页</div>
    <a href="/docs/centos%E6%8C%82%E8%BD%BDiso%E6%96%87%E4%BB%B6%E4%BD%9C%E4%B8%BAyum%E5%AE%89%E8%A3%85%E6%BA%90/" rel="prev">centos挂载iso文件作为yum安装源</a>
  </div>
  
</div>

          </div>
          
        </div>

        <div class="body-footer">
          <p>最近更新于 2022/4/3</p>

          




          




          


        </div>

      </article>

      <footer class="site-footer">

  












  

  

  

  
  






  
  
  

  
  
    
  
  
    
  

  

  
  <p class="powered-by copyright-license-text">
    © 2023 songchuanrang. This work is licensed under <a href="https://creativecommons.org/licenses/by-nc-nd/4.0" rel="noopener noreferrer" target="_blank">CC BY NC ND 4.0</a>
  </p>
  

  <p class="powered-by footer-license-icons">
    <a href="https://creativecommons.org/licenses/by-nc-nd/4.0" rel="noopener noreferrer" target="_blank" aria-label="Creative Commons">
      <i class="fab fa-creative-commons fa-2x" aria-hidden="true"></i>
      <i class="fab fa-creative-commons-by fa-2x" aria-hidden="true"></i>
      
        <i class="fab fa-creative-commons-nc fa-2x" aria-hidden="true"></i>
      
      
        <i class="fab fa-creative-commons-nd fa-2x" aria-hidden="true"></i>
      
    </a>
  </p>




  <p class="powered-by">
    
    
    
      
      
      
      
      
      
      由<a href="https://wowchemy.com/?utm_campaign=poweredby" target="_blank" rel="noopener">Wowchemy</a>支持发布——免费<a href="https://github.com/wowchemy/wowchemy-hugo-themes" target="_blank" rel="noopener">开源</a>网站，为创作者赋能。
    
  </p>
</footer>


    </main>
  </div>
</div>

  </div>

  <div class="page-footer">
    
    
  </div>

  


<script src="/js/vendor-bundle.min.32ee83730ed883becad04bc5170512cc.js"></script>




  

  
  

  









<script src="https://cdn.jsdelivr.net/npm/anchor-js@5.0.0/anchor.min.js" integrity="sha256-aQmOEF2ZD4NM/xt4hthzREIo/2PFkOX/g01WjxEV7Ys=" crossorigin="anonymous"></script>
<script>
  anchors.add();
</script>





  
  <script id="search-hit-fuse-template" type="text/x-template">
    <div class="search-hit" id="summary-{{key}}">
      <div class="search-hit-content">
        <div class="search-hit-name">
          <a href="{{relpermalink}}">{{title}}</a>
          <div class="article-metadata search-hit-type">{{type}}</div>
          <p class="search-hit-description">{{snippet}}</p>
        </div>
      </div>
    </div>
  </script>
  
    <script src="https://cdn.jsdelivr.net/gh/krisk/Fuse@v3.2.1/dist/fuse.min.js" integrity="sha512-o38bmzBGX+hD3JHWUFCDA09btWaqrNmoJ3RXLlrysA7PP01Kgs4UlE4MhelE1v5dJR3+cxlR4qQlotsW7jKsnw==" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/gh/julmot/mark.js@8.11.1/dist/jquery.mark.min.js" integrity="sha512-mhbv5DqBMgrWL+32MmsDOt/OAvqr/cHimk6B8y/bx/xS88MVkYGPiVv2ixKVrkywF2qHplNRUvFsAHUdxZ3Krg==" crossorigin="anonymous"></script>
  












  
  
  
  
  
  
  







<script id="page-data" type="application/json">{"use_headroom":false}</script>











  
  


<script src="/zh/js/wowchemy.min.4e5e27caa14e13784384b84ea7186f46.js"></script>























</body>
</html>
